﻿using System;
using System.IO;
using System.Windows.Forms;
using WebResourcesManager.AppCode;

namespace WebResourcesManager.UserControls
{
    /// <summary>
    /// Control that displays a code web resource
    /// </summary>
    public partial class CodeControl : UserControl, IWebResourceControl
    {
        const string OPENFILE_TITLE_MASK = "Select the {0} to include in the web resource";

        #region Variables

        /// <summary>
        /// Type of web resource
        /// </summary>
        Enumerations.WebResourceType innerType;

        /// <summary>
        /// Base64 content of the web resource when loading this control
        /// </summary>
        string originalContent;

        /// <summary>
        /// Base64 content of the web resource
        /// </summary>
        string innerContent;

        #endregion Variables

        #region Delegates

        public delegate void WebResourceUpdatedEventHandler(object sender, WebResourceUpdatedEventArgs e);

        #endregion

        #region Event Handlers

        public event WebResourceUpdatedEventHandler WebResourceUpdated;

        #endregion

        #region Constructor

        /// <summary>
        /// Initializes a new instance of class CodeControl
        /// </summary>
        /// <param name="content">Base64 content of the web resource</param>
        /// <param name="type">Web resource type</param>
        public CodeControl(string content, Enumerations.WebResourceType type)
        {
            InitializeComponent();

            // Converts base64 content to string 
            byte[] b = Convert.FromBase64String(content);
            this.innerContent = System.Text.Encoding.UTF8.GetString(b);
            this.originalContent = this.innerContent;
            this.innerType = type;
        }

        #endregion Constructor

        #region Handlers

        private void CodeControl_Load(object sender, EventArgs e)
        {
            this.tecCode.Text = this.innerContent;

            switch (this.innerType)
            {
                case Enumerations.WebResourceType.Script:
                    {
                        this.tecCode.SetHighlighting("JavaScript");
                    }
                    break;
                case Enumerations.WebResourceType.Data:
                    {
                        this.tecCode.SetHighlighting("XML");
                    }
                    break;
                case Enumerations.WebResourceType.WebPage:
                    {
                        this.tecCode.SetHighlighting("HTML");
                    }
                    break;
                case Enumerations.WebResourceType.Css:
                    {
                        this.tecCode.SetHighlighting("HTML");
                    }
                    break;
                case Enumerations.WebResourceType.Xsl:
                    {
                        this.tecCode.SetHighlighting("HTML");
                    }
                    break;
            }
        }

        private void tecCode_TextChanged(object sender, EventArgs e)
        {
            this.innerContent = this.tecCode.Text;
            this.SendSavedMessage();
        }

        #endregion Handlers

        #region Methods

        public string GetBase64WebResourceContent()
        {
            byte[] bytes = System.Text.UTF8Encoding.UTF8.GetBytes(this.tecCode.Text);

            return Convert.ToBase64String(bytes);
        }

        public void MinifyJs()
        {
            this.tecCode.Text = Yahoo.Yui.Compressor.JavaScriptCompressor.Compress(this.tecCode.Text, false, true, false, false, 200);
            this.tecCode.Refresh();
        }

        public void ReplaceWithNewFile()
        {
            using (OpenFileDialog ofd = new OpenFileDialog())
            {
                switch (this.innerType)
                {
                    case Enumerations.WebResourceType.Script:
                        {
                            ofd.Title = string.Format(OPENFILE_TITLE_MASK, "script file");
                        }
                        break;
                    case Enumerations.WebResourceType.WebPage:
                        {
                            ofd.Title = string.Format(OPENFILE_TITLE_MASK, "web page");
                        }
                        break;
                    case Enumerations.WebResourceType.Css:
                        {
                            ofd.Title = string.Format(OPENFILE_TITLE_MASK, "css file");
                        }
                        break;
                    case Enumerations.WebResourceType.Xsl:
                        {
                            ofd.Title = string.Format(OPENFILE_TITLE_MASK, "xsl file");
                        }
                        break;
                }

                if (ofd.ShowDialog(this.ParentForm) == DialogResult.OK)
                {
                    string newContent = Convert.ToBase64String(File.ReadAllBytes(ofd.FileName));
                    this.innerContent = new StreamReader(ofd.FileName).ReadToEnd();

                    this.CodeControl_Load(null, null);

                    this.SendSavedMessage();
                }
            }
        }

        private void SendSavedMessage()
        {
            WebResourceUpdatedEventArgs wrueArgs = new WebResourceUpdatedEventArgs()
            {
                Base64Content = this.innerContent,
                IsDirty = (this.innerContent != this.originalContent),
                Type = this.innerType
            };

            if (this.WebResourceUpdated != null)
            {
                this.WebResourceUpdated(this, wrueArgs);
            }
        }

        #endregion Methods
    }
}